package ro.xblue.translator;

import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.servlet.ServletContext;

import ro.gateway.aida.db.DBPersistenceManager;
import ro.gateway.aida.db.PersistenceToken;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: Translator_DB.java,v 1.1 2004/10/24 23:37:02 mihaipostelnicu Exp $
 */
public class Translator_DB extends DBPersistenceManager
{
      private Translator_DB( PersistenceToken token )
      {
            super( token );
            reloadModules();
      }

      private static Translator_DB instance = null;

      public static Translator_DB getManager( PersistenceToken token )
      {
            if ( instance == null )
            {
                  instance = new Translator_DB( token );
            }
            return instance;
      }

      private Hashtable h_modules = new Hashtable();

      /**
       * Incarca modulul specificat
       * Daca nu a mai fost incarcat, incearca
       * il construieste
       *
       * @param name
       */
      public void reloadModule( String name )
      {
            Module_DB module = ( Module_DB ) h_modules.get( name );
            if ( module != null )
            {
                  module.reload();
                  return;
            }

            module = Module_DB.getManager( db_token, name );
            h_modules.put( name, module );
            make_module_list();
      }

      /**
       * Reincarca modulele
       */
      public void reloadModules()
      {
            h_modules.clear();

            String[] modules = null;
            try
            {
                  modules = TranslatorDBAccessor.getManager( db_token ).
                        getModuleNames();
            }
            catch ( SQLException sqlEx )
            {
                  sqlEx.printStackTrace();
            }

            if ( modules != null )
                  for ( int i = 0; i < modules.length; i++ )
                  {
                        Module_DB module = Module_DB.getManager( db_token, modules[ i ] );
                        h_modules.put( modules[ i ], module );
                  }

            make_module_list();
      }

      public String getMessage( String lang, String module_name, String label )
      {
            Module_DB mod = ( Module_DB ) h_modules.get( module_name );
            if ( mod == null ) return null;

            return mod.getMessage( lang, label );
      }

      public String getMessageWithEditTag( String lang, String module_name, String label,ServletContext sc )
      {
            Module_DB mod = ( Module_DB ) h_modules.get( module_name );
            if ( mod == null ) return null;

            return mod.getMessage( lang, label );
      }

      
      public String[] getModuleNames()
      {
            return module_list;
      }

      private String[] module_list;

      private void make_module_list()
      {
            module_list = new String[ h_modules.size() ];
            Enumeration en = h_modules.keys();
            int i = 0;
            while ( en.hasMoreElements() )
            {
                  module_list[ i++ ] = ( ( Module_DB ) h_modules.get( en.nextElement() ) ).getName();
            }
      }

      public Module_DB getModule( String name )
      {
            if ( name == null ) return null;
            return ( Module_DB ) h_modules.get( name );
      }

      public static void main( String[] a )
      {
      }
}
